package com.ghca.myinfo.system.entities;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.JsonValue;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.util.Set;

/**
 * Created by Administrator on 2017/1/24.
 */
@Entity
public class Permission implements GrantedAuthority{



    @Id
    @GeneratedValue(generator = "id")
    @GenericGenerator(strategy = "uuid2", name = "id")
    private String id;

    private String name;

    private String description;

    private int priority;


    //1为NavTab;
    //2为Dialog
    private int target;

    //1为AJax
    //2为Iframe

    private int mode;

    private String url;

    private String icon;

    //private boolean isParent;

    @ManyToMany
    @JoinTable
    private Set<Role> roles;

    @ManyToOne
    private Permission parent;

    @ManyToOne
    private Permission root;

    private boolean enabled;

    @Override
    public String getAuthority() {
        return name;
    }

    public String getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isEnabled() {
        return enabled;
    }


    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public int getTarget() {
        return target;
    }

    public void setTarget(int target) {
        this.target = target;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Permission getParent() {
        return parent;
    }

    public void setParent(Permission parent) {
        this.parent = parent;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Permission getRoot() {
        return root;
    }

    public void setRoot(Permission root) {
        this.root = root;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    /*  public boolean isParent() {
        return isParent;
    }

    public void setParent(boolean parent) {
        isParent = parent;
    }*/

    @JsonValue
    public JSONObject jsonValue()  {
        JSONObject object = new JSONObject();
        object.put("id", getId());
        Permission parent = getParent();
        object.put("pid", parent==null?"":parent.getId());
        object.put("text", getName());
        object.put("iconCls", getIcon());
        JSONObject attributes = new JSONObject();
        attributes.put("target", getTarget());
        attributes.put("href", getUrl());
        attributes.put("url",getId());
        attributes.put("mode", getMode());
        object.put("attributes", attributes);
        return object;
    }
}
